<template>
  <base-dialog title="添加便笺"
               v-bind="$attrs" v-on="$listeners" size="tiny">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="80px">
            <el-form-item label="内容" prop="title">
                <el-input v-model="formData.title" type="textarea" placeholder="请输入内容"
                          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
            <div style="margin-left: 80px; margin-bottom: 5px">
                <span style="color: #ff4d50">注：接收人为空，为对所有人可见</span>
            </div>
            <el-form-item label="接收人" prop="recipient">
                <select-any-user v-model="formData.recipient"></select-any-user>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose">取 消</el-button>
    <el-button type="primary" @click="handelConfirm">确 定</el-button>
  </span>
  </base-dialog>
</template>

<script>
    import eventBus from "@/utils/eventBus";
    import BaseDialog from "@/components/Common/BaseDialog";
    import SelectAnyUser from "../../../components/Common/SelectAnyUser";
    import {mapGetters} from "vuex";
    export default {
        name: "AddNotesDialog",
        components: {SelectAnyUser, BaseDialog},
        data() {
            return {
                dialogVisible: false,
                formData: {
                    title: undefined,
                    recipient: undefined
                },
                rules: {
                    title: [{
                        required: true,
                        message: '请输入内容',
                        trigger: 'blur'
                    }],
                },
                search_value:undefined,
            };
        },
        props: ["id"],
        computed:{
          ...mapGetters(["recipient"]),
        },
        created() {
            this.formData.document = this.id
            this.formData.recipient = this.recipient
        },
        methods: {
            handleClose(done) {
                this.$emit('update:visible', false)
            },
            handelConfirm() {
                this.$refs['elForm'].validate(valid => {
                    if (!valid) return
                    try{
                        if (this.formData.recipient == this.recipient) {
                            this.toSendNode();
                        } else {
                            this.$confirm("小纸条默认接收人变更， 是否继续？", "提示", {
                                confirmButtonText: "确定",
                                cancelButtonText: "取消",
                                type: "warning"
                            }).then(() => {
                                    this.toSendNode();
                            })
                        }
                    }catch (e) {
                        this.toSendNode();
                    }
                })
            },
            toSendNode() {
              this.$store.dispatch("notes/toSendNode",{
                title: this.formData.title,
                recipient: this.formData.recipient,
                document:this.id
              } ).then(res=>{
                this.msgSuccess(res.msg)
                this.handleClose()
                eventBus.$emit("loadNotices");
              })
            },
        }
    };
</script>
<style lang="scss" scoped>
    ::v-deep .el-dialog .el-dialog__body{
            padding: 20px 20px 0px 0px;
    }
</style>
